//
//  UserInfo.m
//  Friendsback
//
//  Created by 宋质彬 on 2018/3/8.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "UserInfo.h"

@implementation UserInfo
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper{
    
    return @{@"accountID":@"id",
             };
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int count = 0;
    Ivar *vars = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = vars[i];
        const char *name = ivar_getName(ivar);
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
    free(vars);
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    unsigned int count = 0;
    if (self = [super init]) {
        Ivar *vars = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i++) {
            Ivar var = vars[i];
            const char *name = ivar_getName(var);
            //归档
            NSString *key = [NSString stringWithUTF8String:name];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(vars);
    }
    return self;
}

- (NSString *)description {
    unsigned int count = 0;
    objc_property_t * property_t = class_copyPropertyList([self class], &count);
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:count];
    for (unsigned int i = 0; i < count; i ++) {
        objc_property_t pty = property_t[i];
        const char *name = property_getName(pty);
        NSString * nameString = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:nameString] ? : @"null";
        [dic setObject:value forKey:nameString];
    }
    free(property_t);
    return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dic];
}
@end
